#include <my_header.h>
#include <crypt.h>
/* Usage*/
 typedef struct clientLogTain_s{
         int length;
         char data[1000];
     }clientLogTrain_t;
 

int sendLogin(int sockfd)
{ 
    clientLogTrain_t train;
    int login_succss = 0;
    char sendName[1024];
    while(login_succss == 0){

        printf("请输入用户名：\n");
        fgets(sendName,sizeof(sendName),stdin);
        sendName[strlen(sendName)-1] = '\0';
    
        train.length = strlen(sendName) + 1;
        strcpy(train.data,sendName);
        send(sockfd,&train.length,sizeof(train.length),0);
        send(sockfd,train.data,train.length,0);

        //接受用户是否存在
        bzero(&train,sizeof(train));
        recv(sockfd,&train.length,sizeof(train.length),0);
        recv(sockfd,train.data,train.length,0);

        if(strcmp(train.data,"用户不存在！") == 0){ 
            printf("用户不存在，请重新输入\n");
            continue;
        } else if(strcmp(train.data,"用户存在！") == 0){ 
            char pswdBuf[1024];
            printf("用户存在,");

            //接收服务端发来的盐值
            bzero(&train,sizeof(train));
            char salt[4096];
            recv(sockfd,&train.length,sizeof(train.length),0);
            recv(sockfd,train.data,train.length,0);
            strcpy(salt,train.data);

            int pswd_succ = 0;
            while(pswd_succ == 0 && login_succss == 0){  
                printf("请输入密码：\n");
                fflush(stdin);
                fgets(pswdBuf,sizeof(pswdBuf),stdin);
                pswdBuf[strlen(pswdBuf)-1] = '\0';


                char *new_crypt = crypt(pswdBuf, salt);
                /* printf("new_crypt =%s\n",new_crypt);//测试 */
                
                //发送加密的密文密码
                bzero(&train,sizeof(train));
                train.length = strlen(new_crypt) + 1;
                strcpy(train.data,new_crypt);
                send(sockfd,&train.length,sizeof(train.length),0);
                send(sockfd,train.data,train.length,0);

                //接收登陆是否成功的火车
                bzero(&train,sizeof(train));
                char sucMsg[1024];
                recv(sockfd,&train.length,sizeof(train.length),0);
                recv(sockfd,train.data,train.length,0);
                strcpy(sucMsg,train.data);

                if(strcmp(sucMsg,"登陆成功！") == 0)
                { 
                    printf("%s\n",sucMsg);
                    pswd_succ = 1;
                    login_succss = 1;
                    fflush(stdin);
                    return 1;
                }
                else if(strcmp(sucMsg,"尝试次数已用完！")==0)
                { 
                    printf("%s\n", sucMsg);
                    printf("请重新登录！\n");
                    break;  // 跳出密码循环，回到用户名输入
                }
                else{ 
                    printf("密码错误，请重新输入：\n");
                }
            }
        }
    }
    fflush(stdin);
    return 0;
}

/* int main(int argc,char *argv[]) */
/* { */
/*    ARGS_CHECK(argc,3); */
/*    int sockfd = socket(AF_INET,SOCK_STREAM,0); */
/*    ERROR_CHECK(sockfd,-1,"socket"); */
/*    struct sockaddr_in addr; */
/*    addr.sin_family = AF_INET; */
/*    addr.sin_port = htons(atoi(argv[2])); */
/*    addr.sin_addr.s_addr = inet_addr(argv[1]); */

/*    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)); */
/*    ERROR_CHECK(ret,-1,"connect"); */

/*    sendLogin(sockfd); */
/*    close(sockfd); */

/*     return 0; */
/* } */

